/**
 * Copyright (C), 2020-2021
 * FileName: LoginContrller
 * Author:   86189
 * Date:     2021/3/8 22:00
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.itheima.wallet.web.controller;

import com.itheima.user.pojo.User;
import com.itheima.wallet.web.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author 86189
 * @create 2021/3/8
 * @since 1.0.0
 */
@RestController
@RequestMapping("/login")
public class LoginContrller {

    @Autowired
    private LoginService loginService;

    /**
     * 获取验证码
     *
     * @param phone
     * @return
     */
    @PostMapping("/sendCode")
    public String loginCode(@RequestParam("phone") String phone) {
        String code = this.loginService.sendCode(phone);
        return code;
    }

    /**
     * 注册用户
     *
     * @param param
     * @return
     */
    @PostMapping("/zhuce")
    public String zhuce(@RequestBody Map<String, String> param) {
        String username = param.get("username").toString();
        String phone = param.get("phone").toString();
        String password = param.get("password").toString();
        String code = param.get("code").toString();

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setPhone(phone);

        String token = null;

        token = loginService.zhuce(user, code);

        return token;
    }

    /**
     * 登录
     *
     * @param param
     * @return
     */
    @PostMapping("/denglu")
    public String denglu(@RequestBody Map<String, String> param) {
        String phone = param.get("phone").toString();
        String code = param.get("code").toString();

        User user = new User();
        user.setPhone(phone);

        String token = null;

        token = loginService.denglu(phone, code);

        return token;
    }

}
